package com.gt.web.srv.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gt.web.api.common.R;
import com.gt.web.srv.domain.RepairApplication;
import com.gt.web.srv.domain.RepairApplicationImages;
import com.gt.web.srv.enums.RepairStatusEnum;
import com.gt.web.srv.mapper.RepairApplicationImagesMapper;
import com.gt.web.srv.mapper.RepairApplicationMapper;
import com.gt.web.srv.mapstructs.RepairApplicationStruct;
import com.gt.web.srv.service.RepairApplicationService;
import com.gt.web.srv.web.api.command.RepairApplicationCommand;
import com.gt.web.srv.web.api.resource.ss.RepairApplicationResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author family
* @createDate 2024-06-05 10:38:36
*/
@Service
public class RepairApplicationServiceImpl implements RepairApplicationService {

	@Autowired
	private RepairApplicationMapper repairApplicationMapper;
	@Autowired
	private RepairApplicationImagesMapper repairApplicationImagesMapper;


	@Override
	public R insert(RepairApplicationCommand repairApplication) {
		RepairApplication application = RepairApplicationStruct.INSTANCE.simple(repairApplication);
		repairApplicationMapper.insert(application);
		return R.OK;
	}

	@Override
	public R<RepairApplicationResource> selectById(Long repairId) {
		RepairApplication repairApplication = repairApplicationMapper.selectById(repairId);
		RepairApplicationResource application = RepairApplicationStruct.INSTANCE.simple(repairApplication);

		if (Objects.nonNull(application)) {
			List<RepairApplicationImages> repairApplicationImages = repairApplicationImagesMapper.selectByRepairId(repairId);
			List<RepairApplicationResource.RepairApplicationImageDto> imageDtos=RepairApplicationStruct.INSTANCE.simpleImages(repairApplicationImages);
			application.setImageDtos(imageDtos);
		}
		return R.ok(application);
	}

	@Override
	public R updateById(RepairApplicationCommand repairApplication) {
		RepairApplication application = RepairApplicationStruct.INSTANCE.simple(repairApplication);
		repairApplicationMapper.update(application);
		return R.OK;
	}

	/**
	 * 更新状态和物流信息
	 * @param repairId 维修ID
	 * @param trackingNumber 快递单号
	 * @param updateBy 更新人
	 * @return
	 */
	@Override
	public R updateStatusAndTracking(Long repairId, String trackingNumber, String updateBy) {
		repairApplicationMapper.updateStatusAndTracking(repairId,  RepairStatusEnum.SHIPMENT.getState(),  trackingNumber, updateBy);
		return R.OK;
	}

	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@Override
	public R<PageInfo<RepairApplicationResource>> page(RepairApplicationCommand request) {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<RepairApplication> repairApplications = repairApplicationMapper.selectByUserId(request.getUserId());
		List<RepairApplicationResource> repairApplicationResources = RepairApplicationStruct.INSTANCE.simpleList(repairApplications);
		return R.ok(new PageInfo<>(repairApplicationResources));
	}


}




